package main

import (
	"OnlineUpdateService/OssClinet"
	"fmt"
	"log"
)

func main() {
	cfg := OssClinet.Config{
		Endpoint:        "oss-cn-hangzhou.aliyuncs.com",
		AccessKeyID:     "LTAI5tC4EkJZ4ejgeaeRV2bw",
		AccessKeySecret: "3tjA8gYOp8Mivoj3T6OyckFyr1g3e1",
	}

	client, err := OssClinet.NewClient(cfg)
	if err != nil {
		log.Fatalf("create client: %v", err)
	}

	fmt.Println("==== List Buckets ====")
	buckets, err := client.ListBuckets()
	if err != nil {
		log.Fatalf("list buckets: %v", err)
	}
	for _, b := range buckets {
		fmt.Println(b.Name)
	}

	bucketName := "test-bucket-sample-2025"
	fmt.Println("==== Create Bucket ====")
	if err := client.CreateBucket(bucketName); err != nil {
		log.Fatalf("create bucket: %v", err)
	}

	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("get bucket: %v", err)
	}

	fmt.Println("==== Put Object ====")
	if err := bucket.PutObjectFromFile("hello.txt", "./hello.txt"); err != nil {
		log.Fatalf("put object: %v", err)
	}

	fmt.Println("==== List Objects ====")
	objs, err := bucket.ListObjects("", 100, "")
	if err != nil {
		log.Fatalf("list objects: %v", err)
	}
	for _, o := range objs {
		fmt.Println(o.Key)
	}

	fmt.Println("==== Get Object ====")
	if err := bucket.GetObjectToFile("hello.txt", "./downloaded.txt"); err != nil {
		log.Fatalf("get object: %v", err)
	}

	fmt.Println("==== Delete Object ====")
	if err := bucket.DeleteObject("hello.txt"); err != nil {
		log.Fatalf("delete object: %v", err)
	}

	fmt.Println("==== Delete Bucket ====")
	if err := client.DeleteBucket(bucketName); err != nil {
		log.Fatalf("delete bucket: %v", err)
	}

	fmt.Println("All operations completed successfully!")
}
